package arch.chameleon.module.security.realm.web.filter;

import static arch.chameleon.support.constants.WebConstants.REQ_BODY;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.authc.AuthenticationToken;

import arch.chameleon.module.security.token.CaptchaUsernamePasswordToken;


/**
 * 在CustomFormAuthenticationFilter基础上整合验证码
 * @author caobin
 *
 */
public class CaptchaFormAuthenticationFilter extends
		CustomFormAuthenticationFilter {

	public CaptchaFormAuthenticationFilter(){

	}
	
	@Override
	protected AuthenticationToken createToken(ServletRequest request,
			ServletResponse response) {
		
		String strUsername = (String)((Map)request.getAttribute(REQ_BODY)).get(usernameParamName);
		String strPassword = (String)((Map)request.getAttribute(REQ_BODY)).get(passwordnameParamName);
		String strCaptcha = (String)((Map)request.getAttribute(REQ_BODY)).get(captchaParamName);
		
		boolean bRememberMe = isRememberMe(request);
		
		String host = getHost(request);

		return new CaptchaUsernamePasswordToken(strUsername, strPassword, 
				bRememberMe, host, strCaptcha);
	}
	
	private String usernameParamName;
	
	private String passwordnameParamName;
	
	private String captchaParamName;

	public void setUsernameParamName(String usernameParamName) {
		this.usernameParamName = usernameParamName;
	}

	public void setPasswordnameParamName(String passwordnameParamName) {
		this.passwordnameParamName = passwordnameParamName;
	}

	public void setCaptchaParamName(String captchaParamName) {
		this.captchaParamName = captchaParamName;
	}

}
